//
//  diZhiView.m
//  jinling
//
//  Created by mac on 2018/10/8.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "diZhiView.h"

@implementation diZhiView

-(void)creatView{
    self.backgroundColor=[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];

    
    
    UIView *beijingView=[UIView new];
    [self addSubview:beijingView];
    [beijingView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.mas_equalTo(0);
        make.height.mas_equalTo(_mainH*0.4+40);
    }];
    beijingView.backgroundColor=[UIColor whiteColor];
    
    
    UIButton *queDing=[UIButton new];
    [beijingView addSubview:queDing];
    [queDing mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(50);
    }];
    [queDing setTitle:@"取消" forState:UIControlStateNormal];
    queDing.titleLabel.font=[UIFont systemFontOfSize:18];
    [queDing setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [queDing addTarget:self action:@selector(quXiao:) forControlEvents:UIControlEventAllEvents];
    
     UIButton *quXiao=[UIButton new];
    [beijingView addSubview:quXiao];
    [quXiao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.height.width.mas_equalTo(queDing);
        make.right.mas_equalTo(-10);
    }];

    [quXiao setTitle:@"确定" forState:UIControlStateNormal];
    quXiao.titleLabel.font=[UIFont systemFontOfSize:18];
    [quXiao setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    quXiao.layer.cornerRadius=5;
    quXiao.layer.borderWidth=1;
    quXiao.layer.borderColor=_backgroundColor.CGColor;
    [quXiao addTarget:self action:@selector(queDing:) forControlEvents:UIControlEventAllEvents];
    

    _dataArr2=[NSMutableArray arrayWithCapacity:0];
    _dataArr3=[NSMutableArray arrayWithCapacity:0];
    _selctDic=[NSMutableArray arrayWithCapacity:0];
     
 
    NSDictionary *dic2=self.dataArr[0];
    NSArray *arr=[dic2 objectForKey:@"children"];
    if (arr.count!=0) {
        [_dataArr2 addObjectsFromArray:arr];
        NSDictionary *dic4=arr[0];
        NSArray *arr3=[dic4 objectForKey:@"children"];
        if (arr3!=nil) {
            [_dataArr3 addObjectsFromArray:arr3];
        }
        NSString *valueDic=[dic2 objectForKey:@"value"];
        NSString *text=[dic2 objectForKey:@"text"];
        
        NSDictionary *dicac=[NSDictionary dictionaryWithObjectsAndKeys:valueDic,@"value",text,@"text", nil];
        [_selctDic addObject:dicac];
        
        NSString *valueDic2=[dic4 objectForKey:@"value"];
        NSString *text2=[dic4 objectForKey:@"text"];
        
        NSDictionary *dicac2=[NSDictionary dictionaryWithObjectsAndKeys:valueDic2,@"value",text2,@"text", nil];
        [_selctDic addObject:dicac2];
        
        NSDictionary *dic5=arr3[0];
        
        NSString *valueDic3=[dic5 objectForKey:@"value"];
        NSString *text3=[dic5 objectForKey:@"text"];
        
        NSDictionary *dicac3=[NSDictionary dictionaryWithObjectsAndKeys:valueDic3,@"value",text3,@"text", nil];
        [_selctDic addObject:dicac3];
    }
    
    _pickerView = [UIPickerView new];
    [beijingView addSubview:_pickerView];
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
         make.height.mas_equalTo(_mainH*0.4);
    }];
    _pickerView.dataSource = self;
    _pickerView.delegate = self;
    _pickerView.showsSelectionIndicator = YES;
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 3;// 返回2表明该控件只包含2列
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    if (component == 0) {
        return self.dataArr.count;
    }else if (component == 1){
        return self.dataArr2.count;
    }else if (component == 2){
        return self.dataArr3.count;
    }
    return 0;
}


-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    UILabel *lbl = (UILabel *)view;
    
    if (lbl == nil) {
        
        lbl = [[UILabel alloc]init];
        
        //在这里设置字体相关属性
        
        lbl.font = [UIFont systemFontOfSize:16];

        [lbl setTextAlignment:NSTextAlignmentCenter];
    
        
        if (component == 0) {
            lbl.text=self.dataArr[row][@"text"];
        }
        if (component == 1){
            lbl.text=self.dataArr2[row][@"text"];
        }
        if (component == 2){
            lbl.text=self.dataArr3[row][@"text"];
        }
        

    }
    return lbl;
        
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 45.0;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
    
        [_dataArr2 removeAllObjects];
        [_dataArr3 removeAllObjects];
        
        
        NSDictionary *dic2=self.dataArr[row];

        
        NSString *valueDic=[dic2 objectForKey:@"value"];
        NSString *text=[dic2 objectForKey:@"text"];
        
        NSDictionary *Dic=[NSDictionary dictionaryWithObjectsAndKeys:valueDic,@"value",text,@"text", nil];
        
        [_selctDic setObject:Dic atIndexedSubscript:0];
        
      
        
        if (row>=_dataArr.count-3) {
            
            [pickerView selectRow:0 inComponent:1 animated:YES];
            [pickerView reloadComponent:1];
            [pickerView selectRow:0 inComponent:2 animated:YES];
            [pickerView reloadComponent:2];

            return;
        }
      
        NSArray *arr=[dic2 objectForKey:@"children"];

        [_dataArr2 addObjectsFromArray:arr];
        
        NSDictionary *dic4=arr[0];
        
        NSArray *arr3=[dic4 objectForKey:@"children"];
        
        [_dataArr3 addObjectsFromArray:arr3];
        
        NSString *valueDic2=[dic4 objectForKey:@"value"];
        NSString *text2=[dic4 objectForKey:@"text"];
        
        NSDictionary *Dic2=[NSDictionary dictionaryWithObjectsAndKeys:valueDic2,@"value",text2,@"text", nil];
        
       [_selctDic setObject:Dic2 atIndexedSubscript:1];
        
        NSDictionary *dic5=arr3[0];
        
        NSString *valueDic3=[dic5 objectForKey:@"value"];
        NSString *text3=[dic5 objectForKey:@"text"];
        
        NSDictionary *Dic3=[NSDictionary dictionaryWithObjectsAndKeys:valueDic3,@"value",text3,@"text", nil];

        
       [_selctDic setObject:Dic3 atIndexedSubscript:2];
        
//        NSLog(@"%@",[[_selctDic description] kdtk_stringByReplaceingUnicode]);
//
//        NSLog(@"%@",[[dic2 description] kdtk_stringByReplaceingUnicode]);
        
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        [pickerView reloadComponent:2];

       
    }else if (component == 1){
        if (_dataArr2.count==0) {
            [pickerView reloadComponent:2];
            [pickerView selectRow:0 inComponent:2 animated:YES];
            return;
        }
        
        NSDictionary *dic4=_dataArr2[row];
        
        
        NSString *valueDic2=[dic4 objectForKey:@"value"];
        NSString *text2=[dic4 objectForKey:@"text"];
        
        NSDictionary *Dic2=[NSDictionary dictionaryWithObjectsAndKeys:valueDic2,@"value",text2,@"text", nil];
        
        [_selctDic setObject:Dic2 atIndexedSubscript:1];
        
        
        NSArray *arr3=[dic4 objectForKey:@"children"];
        
        NSDictionary *dic5=arr3[0];
        
        
        NSString *valueDic3=[dic5 objectForKey:@"value"];
        NSString *text3=[dic5 objectForKey:@"text"];
        
        NSDictionary *Dic3=[NSDictionary dictionaryWithObjectsAndKeys:valueDic3,@"value",text3,@"text", nil];
        
        [_selctDic setObject:Dic3 atIndexedSubscript:2];
        
        
        
        [_dataArr3 removeAllObjects];
        [_dataArr3 addObjectsFromArray:self.dataArr2[row][@"children"]];
        
    

        
        [pickerView reloadComponent:2];
        [pickerView selectRow:0 inComponent:2 animated:YES];
    }
    
    else if (component == 2){
        if (_dataArr3.count==0) {
   
            return;
        }
        
        NSDictionary *dic5=_dataArr3[row];
        
        NSString *valueDic3=[dic5 objectForKey:@"value"];
        NSString *text3=[dic5 objectForKey:@"text"];
        
        NSDictionary *Dic3=[NSDictionary dictionaryWithObjectsAndKeys:valueDic3,@"value",text3,@"text", nil];
        
        [_selctDic setObject:Dic3 atIndexedSubscript:2];
     
        
    }
    
}
-(IBAction)quXiao:(id)sender{
    
    [self removeFromSuperview];

}

-(void)queDing:(UIButton *)btn{
    _customSelectedBlock(_selctDic);
    [self removeFromSuperview];

}

@end
